// ============================================================================
//
// Copyright (C) 2006-2008 Dengues
//
// Google Group: http://groups.google.com/group/dengues
//
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.reports.editor.design.ruler;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;

import org.dengues.reports.editor.design.cmds.MoveGuideCommand;
import org.dengues.reports.editor.design.models.ReportsDiagram;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.rulers.RulerChangeListener;
import org.eclipse.gef.rulers.RulerProvider;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf Qiang.Zhang.Adolf@gmail.com 2008-7-31 qiang.zhang $
 * 
 */
public class RepEditorRulerProvider extends RulerProvider {

    private RepEditorRuler ruler;

    private Rectangle layoutSize = null;

    private ReportsDiagram diagram;

    /**
     * Getter for layoutSize.
     * 
     * @return the layoutSize
     */
    public Rectangle getLayoutSize() {
        return this.layoutSize;
    }

    /**
     * Sets the layoutSize.
     * 
     * @param layoutSize the layoutSize to set
     */
    public void setLayoutSize(Rectangle layoutSize) {
        this.layoutSize = layoutSize;
    }

    private PropertyChangeListener rulerListener = new PropertyChangeListener() {

        public void propertyChange(PropertyChangeEvent evt) {
            if (evt.getPropertyName().equals(RepEditorRuler.PROPERTY_CHILDREN)) {
                RepEditorGuide guide = (RepEditorGuide) evt.getNewValue();
                if (getGuides().contains(guide)) {
                    guide.addPropertyChangeListener(guideListener);
                } else {
                    guide.removePropertyChangeListener(guideListener);
                }
                for (int i = 0; i < listeners.size(); i++) {
                    ((RulerChangeListener) listeners.get(i)).notifyGuideReparented(guide);
                }
            } else {
                for (int i = 0; i < listeners.size(); i++) {
                    ((RulerChangeListener) listeners.get(i)).notifyUnitsChanged(ruler.getUnit());
                }
            }
        }
    };

    private PropertyChangeListener guideListener = new PropertyChangeListener() {

        public void propertyChange(PropertyChangeEvent evt) {
            if (evt.getPropertyName().equals(RepEditorGuide.PROPERTY_CHILDREN)) {
                for (int i = 0; i < listeners.size(); i++) {
                    ((RulerChangeListener) listeners.get(i)).notifyPartAttachmentChanged(evt.getNewValue(), evt.getSource());
                }
            } else {
                for (int i = 0; i < listeners.size(); i++) {
                    ((RulerChangeListener) listeners.get(i)).notifyGuideMoved(evt.getSource());
                }
            }
        }
    };

    /**
     * Qiang.Zhang.Adolf@gmail.com SubjectRulerProvider constructor comment.
     * 
     * @param ruler
     */
    public RepEditorRulerProvider(ReportsDiagram diagram, boolean isTop) {
        this.diagram = diagram;
        ruler = new RepEditorRuler(isTop);
        ruler.addPropertyChangeListener(rulerListener);
        List<RepEditorGuide> guides = ruler.getGuides();
        for (RepEditorGuide repEditorGuide : guides) {
            repEditorGuide.addPropertyChangeListener(guideListener);
        }
        initLayoutSize(diagram);
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "initLayoutSize".
     * 
     * @param diagram
     */
    private void initLayoutSize(ReportsDiagram diagram) {
        layoutSize = new Rectangle(0, 0, 0, 0);
        if (diagram != null) {
            Dimension dim = RepEditorRulerUtils.getReportsDiagramSize(diagram);
            layoutSize = new Rectangle(0, 0, dim.width, dim.height);
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.rulers.RulerProvider#getRuler()
     */
    @Override
    public Object getRuler() {
        return ruler;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.rulers.RulerProvider#getUnit()
     */
    @Override
    public int getUnit() {
        return ruler.getUnit();
    }

    /**
     * Return null command to forbid create guide beHavior (non-Javadoc)
     * 
     * @see org.eclipse.gef.rulers.RulerProvider#getCreateGuideCommand(int)
     */
    public Command getCreateGuideCommand(int position) {
        return null;
    }

    /**
     * Return null command to forbid delete guide beHavior (non-Javadoc)
     * 
     * @see org.eclipse.gef.rulers.RulerProvider#getCreateGuideCommand(int)
     */

    public Command getDeleteGuideCommand(Object guide) {
        return null;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.rulers.RulerProvider#setUnit(int)
     */
    public void setUnit(int newUnit) {
        ruler.setUnit(newUnit);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.rulers.RulerProvider#getGuidePosition(java.lang.Object)
     */
    public int getGuidePosition(Object guide) {
        return ((RepEditorGuide) guide).getPosition();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.rulers.RulerProvider#getGuides()
     */
    public List getGuides() {
        return ruler.getGuides();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.rulers.RulerProvider#getGuidePositions()
     */
    public int[] getGuidePositions() {
        List guides = getGuides();
        int[] result = new int[guides.size()];
        for (int i = 0; i < guides.size(); i++) {
            result[i] = ((RepEditorGuide) guides.get(i)).getPosition();
        }
        return result;
    }

    /**
     * Return null command to forbid move guide beHavior (non-Javadoc)
     * 
     * @see org.eclipse.gef.rulers.RulerProvider#getCreateGuideCommand(int)
     */
    public Command getMoveGuideCommand(Object obj, int pDelta) {
        RepEditorGuide guide = (RepEditorGuide) obj;
        String propertyName = guide.getPropertyName();
        if (IRepRulerEditorConstants.RIGHT_MARGIN_PROP.equals(propertyName)) {
            pDelta = getLayoutSize().right() - (guide.getPosition() + pDelta);
        } else if (IRepRulerEditorConstants.BOTTOM_MARGIN_PROP.equals(propertyName)) {
            pDelta = getLayoutSize().bottom() - (guide.getPosition() + pDelta);
        } else if (IRepRulerEditorConstants.LEFT_MARGIN_PROP.equals(propertyName)) {
            pDelta = guide.getPosition() + pDelta - getLeftSpace().x;
        } else {
            pDelta = guide.getPosition() + pDelta - getLeftSpace().y;
        }

        return new MoveGuideCommand(diagram, pDelta, guide.getPropertyName());
    }

    /**
     * @param leftSpace The leftSpace to set.
     */
    public void setLeftSpace(Rectangle space) {
        ruler.setLeftSpace(space);
    }

    /**
     * @return
     */
    public Rectangle getLeftSpace() {
        return ruler.getLeftSpace();
    }
}
